package com.hsd.whb.controller;

import com.hsd.whb.converter.DeviceConverter;
import com.hsd.whb.entity.Device;
import com.hsd.whb.entity.Manager;
import com.hsd.whb.entity.Recorder;
import com.hsd.whb.pojo.input.ModifyManager;
import com.hsd.whb.pojo.output.DeviceVO;
import com.hsd.whb.pojo.query.QueryMeeting;
import com.hsd.whb.service.DeviceService;
import com.hsd.whb.service.ManagerService;
import com.hsd.whb.service.MeetingService;
import com.hsd.whb.service.RecorderService;
import com.hsd.whb.utils.BeanUtil;
import com.hsd.whb.utils.ResultVOUtil;
import com.hsd.whb.utils.SecurityUtil;
import com.hsd.whb.utils.ValidatorUtil;
import com.hsd.whb.pojo.output.MeetingVO;
import com.hsd.whb.pojo.output.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/manager")
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    @Autowired
    private RecorderService recorderService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private MeetingService meetingService;

    @GetMapping("/recorderList")
    public String toRecorderList() {
        return "manager/recorderList";
    }

    @GetMapping("/recorderForm")
    public String toRecorderForm() {
        return "manager/recorderForm";
    }


    @PostMapping("/findRecorderList")
    @ResponseBody
    public List<Recorder> findRecorderList(@PageableDefault(sort = "workNumber") Pageable pageable, Recorder recorder) {
        return recorderService.findRecorderList(pageable, SecurityUtil.getUsername(), recorder);
    }

    @GetMapping("/findRecorderList")
    @ResponseBody
    public long findRecorderListTotalPages(Integer size, Recorder recorder) {
        long totalNum = recorderService.findRecorderListTotalPages(SecurityUtil.getUsername(), recorder);
        ;
        return (long) Math.ceil(totalNum / (double) size);
    }

    @GetMapping("/findRecorderById")
    @ResponseBody
    public Recorder findRecorderById(Integer id) {
        return recorderService.findById(id);
    }

    @PostMapping("/updateRecorder")
    @ResponseBody
    public ResultVO updateRecorder(@Valid Recorder recorder, BindingResult bindingResult) {
        ValidatorUtil.valid(bindingResult);
        String msg;
        if (recorder.getId() != null) {
            msg = "修改成功";
            recorderService.updateRecorder(recorder);
        } else {
            msg = "添加成功";
            recorderService.saveRecorder(recorder, SecurityUtil.getUsername());
        }
        return ResultVOUtil.success(msg);
    }

    @PostMapping("/deleteRecorderById")
    @ResponseBody
    public ResultVO deleteRecorderById(Integer id) {
        recorderService.deleteRecorderById(id);
        return ResultVOUtil.success("删除成功");
    }

    @GetMapping("/modifyInfo")
    public String toModifyInfo() {
        return "manager/managerForm";
    }

    @PostMapping("/modifyInfo")
    @ResponseBody
    public ResultVO modifyInfo(@Valid ModifyManager modifyManager, BindingResult bindingResult) {
        ValidatorUtil.valid(bindingResult);
        Manager manager = managerService.findByUsername(SecurityUtil.getUsername());
        BeanUtil.copyProperties(modifyManager, manager);
        managerService.update(manager);
        return ResultVOUtil.success("修改成功");
    }

    @GetMapping("/getOneselfInfo")
    @ResponseBody
    public Manager getOneselfInfo() {
        return managerService.findByUsername(SecurityUtil.getUsername());

    }

    @GetMapping("/deviceList")
    public String toDeviceList() {
        return "manager/deviceList";
    }

    @PostMapping("/findDeviceList")
    @ResponseBody
    public List<DeviceVO> findDeviceList(@PageableDefault(sort = "accessKey") Pageable pageable, Device device) {
        List<Device> list = deviceService.findDeviceList(pageable, SecurityUtil.getUsername(), device);

        return DeviceConverter.convertToDeviceVOList(list, managerService);
    }

    @GetMapping("/findDeviceList")
    @ResponseBody
    public long findDeviceListTotalPages(Integer size, Device device) {
        long totalNum = deviceService.findDeviceListTotalPages(SecurityUtil.getUsername(), device);
        ;
        return (long) Math.ceil(totalNum / (double) size);
    }

    @GetMapping("/deviceForm")
    public String toDeviceForm() {
        return "manager/deviceForm";
    }

    @GetMapping("/findDeviceById")
    @ResponseBody
    public DeviceVO findDeviceById(Integer id) {
        Device device = deviceService.findDeviceById(id);
        if (device == null) return null;
        return DeviceConverter.convertToDeviceVO(device);
    }

    @PostMapping("/unBindingDevice")
    @ResponseBody
    public ResultVO unBindingDevice(Integer id) {
        Device device = deviceService.findDeviceById(id);
        device.setManagerId(null);
        deviceService.updateDevice(device);
        return ResultVOUtil.success("解绑成功");
    }

    @PostMapping("/bindingDevice")
    @ResponseBody
    public ResultVO bindingDevice(@Valid Device device, BindingResult bindingResult) {
        ValidatorUtil.valid(bindingResult);
        deviceService.bindingDevice(device, SecurityUtil.getUsername());
        return ResultVOUtil.success("绑定成功");
    }

    @GetMapping("/findDeviceByAccessKy")
    @ResponseBody
    public DeviceVO findDeviceByAccessKy(String key) {
        Device device = deviceService.findDeviceByAccessKey(key);
        if (device == null) return null;
        return DeviceConverter.convertToDeviceVO(device);
    }

    @GetMapping("/fileRecordList")
    public String toFileRecordList() {
        return "manager/fileRecordList";
    }

    @PostMapping("/findFileRecordList")
    @ResponseBody
    public List<MeetingVO> findFileRecordList(@PageableDefault(sort = "startTime", direction = Sort.Direction.DESC) Pageable pageable,
                                              QueryMeeting queryMeeting) {
        return meetingService.findFileRecordList(pageable, SecurityUtil.getUsername(), queryMeeting);
    }

    @GetMapping("/findFileRecordList")
    @ResponseBody
    public long findFileRecordListTotalPages(Integer size, QueryMeeting queryMeeting) {
        long totalNum = meetingService.findFileRecordListTotalPages(SecurityUtil.getUsername(), queryMeeting);
        ;
        return (long) Math.ceil(totalNum / (double) size);
    }

}
